package com.cimcitech.printertestdemo;

import android.annotation.SuppressLint;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cimcitech.printertestdemo.receiver.USBDeviceReceiver;
import com.cimcitech.printertestdemo.utils.PrintUtils;
import com.cimcitech.printertestdemo.utils.USBHelper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.cimcitech.printertestdemo.utils.PrintUtils.PRODUCE_ID;
import static com.cimcitech.printertestdemo.utils.PrintUtils.VENDER_ID;
import static com.cimcitech.printertestdemo.utils.USBHelper.ACTION_USB_PERMISSION;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_find_all_fail;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_find_this_fail;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_ok;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_open_fail;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_passway_fail;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_permission_fail;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_permission_ok;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_send_data_fail;
import static com.cimcitech.printertestdemo.utils.USBInterface.usb_send_data_ok;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    @BindView(R.id.btn_connection)
    Button btnConnection;
    @BindView(R.id.btn_close)
    Button btnClose;
    @BindView(R.id.btn_str)
    Button btnStr;
    @BindView(R.id.btn_print_and_wrap)
    Button btnPrintAndWrap;
    USBDeviceReceiver reciver;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.btn_init_print)
    Button btnInitPrint;
    @BindView(R.id.btn_print_barcode)
    Button btnPrintBarcode;
    @BindView(R.id.btn_print_barcode2)
    Button btnPrintBarcode2;
    @BindView(R.id.btn_print_and_Paper)
    Button btnPrintAndPaper;
    @BindView(R.id.btn_print_and_Paper_feed)
    Button btnPrintAndPaperFeed;
    @BindView(R.id.btn_print_mode0)
    Button btnPrintMode0;
    @BindView(R.id.btn_print_mode1)
    Button btnPrintMode1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        reciver = new USBDeviceReceiver();

        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);

        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);

        registerReceiver(reciver, filter);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        USBHelper.getInstance(this).close();
        if (reciver != null) {
            unregisterReceiver(reciver);
        }

    }


    @SuppressLint("SetTextI18n")
    @OnClick({R.id.btn_connection, R.id.btn_close, R.id.btn_str, R.id.btn_print_and_wrap,
            R.id.btn_init_print, R.id.btn_print_barcode, R.id.btn_print_barcode2,
            R.id.btn_print_and_Paper, R.id.btn_print_and_Paper_feed, R.id.btn_print_mode0, R.id.btn_print_mode1})
    void onListener(View view) {
        switch (view.getId()) {
            case R.id.btn_connection:
//连接设备
                int status = USBHelper.getInstance(this).connection(VENDER_ID, PRODUCE_ID);

                switch (status) {
                    case usb_ok:
                        tvStatus.setText("usb正常打开");
                        break;
                    case usb_permission_ok:
                        tvStatus.setText("USB授权成功");
                        break;
                    case usb_permission_fail:
                        tvStatus.setText("USB授权失败");
                        break;
                    case usb_find_this_fail:
                        tvStatus.setText("没有找到指定设备");
                        break;
                    case usb_find_all_fail:
                        tvStatus.setText("没有找到任何设备");
                        break;
                    case usb_open_fail:
                        tvStatus.setText("USB设备打开失败");
                        break;
                    case usb_passway_fail:
                        tvStatus.setText("USB通道打开失败");
                        break;
                    case usb_send_data_ok:
                        tvStatus.setText("USB发送数据成功");
                        break;
                    case usb_send_data_fail:
                        tvStatus.setText("USB发送数据失败");
                        break;


                    default:
                        break;


                }
                break;
            case R.id.btn_close:
//断开设备连接
                USBHelper.getInstance(this).close();
                tvStatus.setText("usb设备断开");
                break;

            case R.id.btn_init_print:

                printByte(PrintUtils.Print_initialization);
                break;
            case R.id.btn_print_mode0:

                printByte(PrintUtils.Set_print_mode);
                break;
            case R.id.btn_print_mode1:

                printByte(PrintUtils.Set_print_mode1);
                break;


            case R.id.btn_str:
//打印一行测试字符串
                printString("打印一行测试字符串!");
                break;
            case R.id.btn_print_and_wrap:
//打印并换行

                printString("1打印并换行1!");
                printByte(PrintUtils.Print_and_wrap);
                printString("2打印并换行2!");
                break;

            case R.id.btn_print_and_Paper:
//打印并走纸
                printByte(PrintUtils.Print_and_take_paper);
                break;
            case R.id.btn_print_and_Paper_feed:
//打印并进纸

                printByte(PrintUtils.Print_and_feed_paper);

                break;

            case R.id.btn_print_barcode:

                try {
                    onSendBarcode();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                printByte(PrintUtils.Print_barcode);

                break;
            case R.id.btn_print_barcode2:

                try {
                    onSendBarcode2();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                printByte(PrintUtils.Print_barcode);

                break;
            default:
                break;

        }


    }

    private void printString(String str) {

        try {

            byte[] b = str.getBytes("gbk");
            USBHelper.getInstance(this).sendData(b);

        } catch (
                UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private void printByte(byte[] bytes) {

        USBHelper.getInstance(this).sendData(bytes);

    }


    /**
     * 打印条形码
     */
    public void onSendBarcode() throws IOException {

        byte[] bytBarcode = new byte[64];

        bytBarcode[0] = 0x1d;
        bytBarcode[1] = 0x6b;
        bytBarcode[2] = 0x43;
        bytBarcode[3] = 0x0c;
        bytBarcode[4] = 0x39;
        bytBarcode[5] = 0x33;
        bytBarcode[6] = 0x37;
        bytBarcode[7] = 0x30;
        bytBarcode[8] = 0x34;
        bytBarcode[9] = 0x36;
        bytBarcode[10] = 0x33;
        bytBarcode[11] = 0x39;
        bytBarcode[12] = 0x36;
        bytBarcode[13] = 0x33;
        bytBarcode[14] = 0x31;
        bytBarcode[15] = 0x0d;


        USBHelper.getInstance(this).sendData(bytBarcode);


    }


    /**
     *
     * */
    public void onSendBarcode2() throws IOException {
        byte[] bytBarcode = new byte[64];

        //QR code

        bytBarcode[0] = 0x1d;
        bytBarcode[1] = 0x28;
        bytBarcode[2] = 0x31;
        bytBarcode[3] = 0x28;
        bytBarcode[4] = 0x00;
        bytBarcode[5] = 0x00;
        bytBarcode[6] = 0x00;
        bytBarcode[7] = 0x7C;
        bytBarcode[8] = 0x34;
        bytBarcode[9] = 0x36;
        bytBarcode[10] = 0x33;
        bytBarcode[11] = 0x39;
        bytBarcode[12] = 0x36;
        bytBarcode[13] = 0x33;
        bytBarcode[14] = 0x31;
        bytBarcode[15] = 0x67;
        bytBarcode[16] = 0x70;
        bytBarcode[17] = 0x67;
        bytBarcode[18] = 0x67;


        USBHelper.getInstance(this).sendData(bytBarcode);


    }


}
